package com.metadata_system.api_server.ai_server;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.metadata_system.ai.request.*;
import com.metadata_system.ai.service.ProjectService;
import com.metadata_system.api.annotations.AiSystemApiCheck;
import com.metadata_system.common.dto.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RequestMapping("/api/ai-server/v001/Project/")
@RestController("aiServerProjectController")
public class ProjectController {
    @Autowired
    private ProjectService projectService;


    /**
     * @api {post} /api/ai-server/v001/Project/getDatabaseList 01.数据库-列表
     * @apiName 01.数据库-列表
     * @apiGroup 02.项目
     * @apiVersion 1.0.0
     *
     * @apiHeader {String} ApiKey 平台ApiKey
     * @apiHeader {String} Timestamp 时间戳
     * @apiHeader {String} Sign 请求签名
     * @apiHeader {String} Username 用户名(在后台系统查看需要开启【模型工具访问】权限)
     * @apiParam {String} [updatedTime=2025-09-23,2025-09-30] 查询-更新时间段,格式:yyyy-MM-dd,yyyy-MM-dd(可选)
     * @apiParam {String} [page=1] 页码(可选,默认:1)
     * @apiParam {String} [pageSize=100] 每页数量(可选,默认:100)
     * @apiParam {String} [sortProp=updatedTime] 排序字段:updatedTime(可选,默认:updatedTime)
     * @apiParam {String} [sortOrder=desc] 排序顺序:asc/desc(可选,默认:desc)
     *
     * @apiError {Number} code 错误码，非0表示失败
     * @apiError {String} message 错误信息
     *
     * @apiSuccessExample {json} 成功响应:
     * HTTP/1.1 200 OK
     * {
     *     "message": "",
     *     "code": 0,
     *     "data": {
     *         "total": 9,
     *         "deleteIdList": [
     *         ],
     *         "list": [
     *             {
     *                 "id": 1,                                                    // 数据库-id
     *                 "name": "运输车-数据库",                                     // 数据库-名称
     *                 "type": 0,                                                  // 数据库-类型: 0.公共数据库 1.项目数据库 2.个人数据库
     *                 "dataTotal": 0,                                             // 数据库-数据-总数量
     *                 "createdTime": "2025-08-20 10:35:47",                       // 数据库-创建时间
     *                 "updatedTime": "2025-08-20 10:35:47"                        // 数据库-更新时间
     *             },....
     *         ]
     *     },
     *     "extendInfo": [
     *     ],
     *     "timeStamp": 1758586015237,
     *     "execTime": 0,
     *     "error": false
     * }
     */
    @RequestMapping("getDatabaseList")
    @AiSystemApiCheck
    public ApiResult getDatabaseList(
            @RequestParam(required = false) String updatedTime,
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "100") Integer pageSize,
            @RequestParam(required = false, defaultValue = "updatedTime") String sortProp,
            @RequestParam(required = false, defaultValue = "desc") String sortOrder
    ) {
        return projectService.getDatabaseList(DatabaseListRequest.builder()
                .updatedTime(updatedTime)
                .page(page)
                .pageSize(pageSize)
                .sortProp(sortProp)
                .sortOrder(sortOrder)
                .build());
    }


    /**
     * @api {post} /api/ai-server/v001/Project/getProjectList 02.项目-列表
     * @apiName 02.项目-列表
     * @apiGroup 02.项目
     * @apiVersion 1.0.0
     *
     * @apiHeader {String} ApiKey 平台ApiKey
     * @apiHeader {String} Timestamp 时间戳
     * @apiHeader {String} Sign 请求签名
     * @apiHeader {String} Username 用户名(在后台系统查看需要开启【模型工具访问】权限)
     * @apiParam {String} [metadataId=15] 所属元模型ID
     * @apiParam {String} [databaseId=15] 所属数据库ID
     * @apiParam {String} [updatedTime=2025-09-23,2025-09-30] 查询-更新时间段,格式:yyyy-MM-dd,yyyy-MM-dd(可选)
     * @apiParam {String} [page=1] 页码(可选,默认:1)
     * @apiParam {String} [pageSize=100] 每页数量(可选,默认:100)
     * @apiParam {String} [sortProp=updatedTime] 排序字段:updatedTime(可选,默认:updatedTime)
     * @apiParam {String} [sortOrder=desc] 排序顺序:asc/desc(可选,默认:desc)
     *
     * @apiError {Number} code 错误码，非0表示失败
     * @apiError {String} message 错误信息
     *
     * @apiSuccessExample {json} 成功响应:
     * HTTP/1.1 200 OK
     * {
     *     "message": "",
     *     "code": 0,
     *     "data": {
     *         "total": 4,                                                      // 项目-总数量
     *         "deleteIdList": [],                                              // 项目-删除-id列表
     *         "list": [
     *             {
     *                 "id": 7,                                                    // 项目-id
     *                 "databaseId": 7,                                            // 项目-数据库-id
     *                 "databaseName": "工作记录-数据库",                           // 项目-数据库-名称
     *                 "metadataId": 15,                                           // 项目-元模型-id
     *                 "metadataName": "小型运输车-元模型",                         // 项目-元模型-名称
     *                 "name": "小型运输车",                                        // 项目-名称
     *                 "dataTotal": 48,                                            // 项目-数据-总数量
     *                 "description": "小型运输车通常指....动中不可或缺的工具。",     // 项目-描述
     *                 "type": 0,                                                  // 项目-类型: 0.公共数据库 1.项目数据库 2.个人数据库
     *                 "createdTime": "2025-08-22 04:03:58",                       // 项目-创建时间
     *                 "updatedTime": "2025-08-22 06:50:07"                        // 项目-更新时间
     *             },....
     *         ]
     *     },
     *     "extendInfo": [
     *     ],
     *     "timeStamp": 1758585867878,
     *     "execTime": 0,
     *     "error": false
     * }
     */
    @RequestMapping("getProjectList")
    @AiSystemApiCheck
    public ApiResult getProjectList(
            @RequestParam(required = false) Integer metadataId,
            @RequestParam(required = false) Integer type,
            @RequestParam(required = false) Integer databaseId,
            @RequestParam(required = false) String updatedTime,
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "100") Integer pageSize,
            @RequestParam(required = false, defaultValue = "updatedTime") String sortProp,
            @RequestParam(required = false, defaultValue = "desc") String sortOrder
    ) {
        return projectService.getProjectList(ProjectListRequest.builder()
                .type(type)
                .databaseId(databaseId)
                .metadataId(metadataId)
                .updatedTime(updatedTime)
                .page(page)
                .pageSize(pageSize)
                .sortProp(sortProp)
                .sortOrder(sortOrder)
                .build());
    }


    /**
     * @api {post} /api/ai-server/v001/Project/getProjectNodeList 03.项目节点-列表
     * @apiName 03.项目节点-列表
     * @apiGroup 02.项目
     * @apiVersion 1.0.0
     *
     * @apiHeader {String} ApiKey 平台ApiKey
     * @apiHeader {String} Timestamp 时间戳
     * @apiHeader {String} Sign 请求签名
     * @apiHeader {String} Username 用户名(在后台系统查看需要开启【模型工具访问】权限)
     * @apiParam {String} [metadataId=15] 所属元模型ID
     * @apiParam {String} [metadataNodeId=4] 所属元模型节点ID
     * @apiParam {String} [projectId=7] 所属项目ID
     * @apiParam {String} [databaseId=15] 所属数据库ID
     * @apiParam {String} [updatedTime=2025-09-23,2025-09-30] 查询-更新时间段,格式:yyyy-MM-dd,yyyy-MM-dd(可选)
     * @apiParam {String} [page=1] 页码(可选,默认:1)
     * @apiParam {String} [pageSize=100] 每页数量(可选,默认:100)
     * @apiParam {String} [sortProp=updatedTime] 排序字段:updatedTime(可选,默认:updatedTime)
     * @apiParam {String} [sortOrder=desc] 排序顺序:asc/desc(可选,默认:desc)
     *
     * @apiError {Number} code 错误码，非0表示失败
     * @apiError {String} message 错误信息
     *
     * @apiSuccessExample {json} 成功响应:
     * HTTP/1.1 200 OK
     * {
     *     "message": "",
     *     "code": 0,
     *     "data": {
     *         "total": 3,
     *         "deleteIdList": [
     *         ],
     *         "list": [
     *             {
     *                 "id": 17,                                    // 节点-id
     *                 "projectId": 7,                              // 项目-id
     *                 "metadataId": 15,                            // 元模型-id
     *                 "metadataNodeId": 150,                       // 元模型节点-id
     *                 "metadataNodeName": "控制臂",                 // 元模型节点-名称
     *                 "metadataNodeProperties": [                   // 元模型节点-属性列表
     *                     {
     *                         "label": "品牌",                      // 元模型节点-属性-名称
     *                         "type": "string",                    // 元模型节点-属性-类型
     *                         "prop": "brand",                     // 元模型节点-属性-属性名
     *                         "unit": ""                           // 元模型节点-属性-单位
     *                     },....
     *                 ],
     *                 "total": 20,                                  // 数据-总数量
     *                 "createdTime": "2025-09-21 23:21:33",         // 数据-创建时间
     *                 "updatedTime": "2025-09-21 23:21:33"          // 数据-更新时间
     *             }
     *         ]
     *     },
     *     "extendInfo": [
     *     ],
     *     "timeStamp": 1758585751753,
     *     "execTime": 0,
     *     "error": false
     * }
     */
    @RequestMapping("getProjectNodeList")
    @AiSystemApiCheck
    public ApiResult getProjectNodeList(
            @RequestParam(required = false) Integer metadataId,
            @RequestParam(required = false) Integer metadataNodeId,
            @RequestParam(required = false) Integer projectId,
            @RequestParam(required = false) Integer databaseId,
            @RequestParam(required = false) String updatedTime,
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "100") Integer pageSize,
            @RequestParam(required = false, defaultValue = "updatedTime") String sortProp,
            @RequestParam(required = false, defaultValue = "desc") String sortOrder) {
        return projectService.getProjectNodeList(ProjectNodeListRequest.builder()
                .metadataNodeId(metadataNodeId)
                .databaseId(databaseId)
                .metadataId(metadataId)
                .projectId(projectId)
                .updatedTime(updatedTime)
                .page(page)
                .pageSize(pageSize)
                .sortProp(sortProp)
                .sortOrder(sortOrder)
                .build());
    }


    /**
     * @api {post} /api/logic-tools-server/v001/Project/getProjectNodeDataList 04.项目-节点-数据列表
     * @apiName 04.项目-节点-数据列表
     * @apiGroup 02.项目
     * @apiVersion 1.0.0
     *
     * @apiHeader {String} ApiKey 平台ApiKey
     * @apiHeader {String} Timestamp 时间戳
     * @apiHeader {String} Sign 请求签名
     * @apiHeader {String} Username 用户名(在后台系统查看需要开启【模型工具访问】权限)
     * @apiParam {String} [projectId=28] 项目-ID
     * @apiParam {String} [metadataNodeId=4] 元模型节点-ID
     * @apiParam {String} [searchMap] 搜索条件-json,格式:{"name":"中","age":"5,10"},除过number以外,其他字段都是模糊查找,number字段支持范围查找,格式:min,max,createdTime查询格式:yyyy-MM-dd,yyyy-MM-dd HH:mm:ss
     * @apiParam {String} [sortProp=createdTime] 排序字段(可选,默认:createdTime)
     * @apiParam {String} [sortOrder=desc] 排序顺序(可选,默认:desc)
     * @apiParam {String} [page=1] 页码(可选,默认:1)
     * @apiParam {String} [pageSize=20] 每页数量(可选,默认:20)
     *
     * @apiError {Number} code 错误码，非0表示失败
     * @apiError {String} message 错误信息
     *
     * @apiSuccessExample {json} 成功响应:
     * HTTP/1.1 200 OK
     * {
     *     "message": "",
     *     "code": 0,
     *     "data": {
     *         "total": 20,
     *         "deleteIdList":[],                                      // 被删除的ID列表
     *         "list": [
     *             {
     *                 "diameter": 15.5,                                // 数据字段
     *                 "length": 145.0,                                 // 数据字段
     *                 "freeLength": 400.0,                             // 数据字段
     *                 "model": "吉普牧马人 JL 前轴",                    // 数据字段
     *                 "colorCode": "黄色",
     *                 "type": "B8 套件",
     *                 "stiffness": 45.0,
     *                 "brand": "Bilstein",
     *                 "id": 40,                                        // 数据-序号
     *                 "projectLogId": 46,                              // 对应项目日志ID
     *                 "createdTime": "2025-09-22 13:21:32",            // 创建时间
     *             },....
     *         ]
     *     },
     *     "extendInfo": [
     *     ],
     *     "timeStamp": 1758567990183,
     *     "execTime": 0,
     *     "error": false
     * }
     */
    @RequestMapping("getProjectNodeDataList")
    @AiSystemApiCheck
    public ApiResult getProjectNodeDataList(
            @RequestParam(required = false) Integer projectId,
            @RequestParam(required = false) Integer metadataNodeId,
            @RequestParam(required = false) String searchMap,
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "100") Integer pageSize,
            @RequestParam(required = false, defaultValue = "createdTime") String sortProp,
            @RequestParam(required = false, defaultValue = "desc") String sortOrder
            ) {
        Map<String, Object> _searchMap = JSON.parseObject(searchMap, new TypeReference<Map<String, Object>>() {
        });
        return projectService.getProjectNodeDataList(projectId, metadataNodeId, page, pageSize, _searchMap, sortProp, sortOrder);
    }


}
